package com.felix.ocoupon.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author hsufelix
 * @email hsufelix@yeah.net
 * @Date 2020/11/10 23:43
 *
 * <h1>校验请求中传递的TOKEN</h1>
 */
@Slf4j
@Component
public class TokenFilter extends AbstractPreZuulFilter{

    @Override
    protected Object customizeRun() {
        HttpServletRequest request = context.getRequest();
        final String info = String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString());
        log.info(info);
        Object token = request.getParameter("token");
        if (null == token) {
            log.error("error: token is empty");
            return fail(401, "error: token is empty");
        }
        return success();
    }

    @Override
    public int filterOrder() {
        return 1;
    }
}
